//
//  ZXToolObj.m
//  qianke
//
//  Created by ZX on 2017/7/7.
//  Copyright © 2017年 ZX. All rights reserved.
//

#import "ZXToolObj.h"

@implementation ZXToolObj

//判断字符串是否为空格
+ (BOOL) isEmpty:(NSString *) str {
    if (!str) {
        return true;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}

+(NSString * )getTimeF:(NSString *)timeStr{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterFullStyle];// 修改下面提到的北京时间的日期格式
    [formatter setTimeStyle:NSDateFormatterFullStyle];// 修改下面提到的北京时间的时间格式
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    
    // 北京时间戳1500000000（可以把北京时间戳转成北京时间、格林尼治时间）
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:timeStr.integerValue];
    NSLog(@"%@",date1);// 这个时间是格林尼治时间
    NSString *dat = [formatter stringFromDate:date1];
    NSLog(@"%@", dat);// 这个时间是北京时间

    return dat;

}

+ (const char *)UnicodeToISO88591:(NSString *)src
{
    NSStringEncoding enc =      CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatin1);
    return [src cStringUsingEncoding:enc];
}
+ (NSString *)unicode2ISO88591:(NSString *)string {
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatin1);
    
    return [NSString stringWithCString:[string UTF8String] encoding:enc];
    
}


+ (NSString *)iphoneType {
    
    struct utsname systemInfo;
    
    uname(&systemInfo);
    
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
    
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
    
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
    
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
    
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    
    if ([platform isEqualToString:@"iPod1,1"])  return @"iPod Touch 1G";
    
    if ([platform isEqualToString:@"iPod2,1"])  return @"iPod Touch 2G";
    
    if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
    
    if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
    
    if ([platform isEqualToString:@"iPod5,1"])  return @"iPod Touch 5G";
    
    if ([platform isEqualToString:@"iPad1,1"])  return @"iPad 1G";
    
    if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,5"])  return @"iPad Mini 1G";
    
    if ([platform isEqualToString:@"iPad2,6"])  return @"iPad Mini 1G";
    
    if ([platform isEqualToString:@"iPad2,7"])  return @"iPad Mini 1G";
    
    if ([platform isEqualToString:@"iPad3,1"])  return @"iPad 3";
    
    if ([platform isEqualToString:@"iPad3,2"])  return @"iPad 3";
    
    if ([platform isEqualToString:@"iPad3,3"])  return @"iPad 3";
    
    if ([platform isEqualToString:@"iPad3,4"])  return @"iPad 4";
    
    if ([platform isEqualToString:@"iPad3,5"])  return @"iPad 4";
    
    if ([platform isEqualToString:@"iPad3,6"])  return @"iPad 4";
    
    if ([platform isEqualToString:@"iPad4,1"])  return @"iPad Air";
    
    if ([platform isEqualToString:@"iPad4,2"])  return @"iPad Air";
    
    if ([platform isEqualToString:@"iPad4,3"])  return @"iPad Air";
    
    if ([platform isEqualToString:@"iPad4,4"]) return @"iPad Mini 2G";
    
    if ([platform isEqualToString:@"iPad4,5"])  return @"iPad Mini 2G";
    
    if ([platform isEqualToString:@"iPad4,6"])return @"iPad Mini 2G";
    
    if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
    
    if ([platform isEqualToString:@"x86_64"])return @"iPhone Simulator";
    
    return platform;
    
}
+(NSString *)today{
    // 获取代表公历的NSCalendar对象
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    // 获取各时间字段的数值
//    NSLog(@"现在是%ld年" , comp.year);
//    NSLog(@"现在是%ld月 " , comp.month);
//    NSLog(@"现在是%ld日" , comp.day);
//    NSLog(@"现在是%ld时" , comp.hour);
//    NSLog(@"现在是%ld分" , comp.minute);
//    NSLog(@"现在是%ld秒" , comp.second);
//    NSLog(@"现在是星期%ld" , comp.weekday);
    // 再次创建一个NSDateComponents对象
//    NSDateComponents* comp2 = [[NSDateComponents alloc]
//                               init];
    // 设置各时间字段的数值
    //    comp2.year = 2013;
    //    comp2.month = 4;
    //    comp2.day = 5;
    comp.hour = 00;
    comp.minute = 00;
    comp.second = 00;
    // 通过NSDateComponents所包含的时间字段的数值来恢复NSDate对象
//    NSDate *date = [gregorian dateFromComponents:comp];
//    NSLog(@"获取的日期为：%@" , date);
    NSString * today = [NSString stringWithFormat:@"%ld-%ld-%ld",(long)comp.year,(long)comp.month,(long)comp.day];
    return today;
}
+(NSString *)todayBegin{
    // 获取代表公历的NSCalendar对象
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    // 获取各时间字段的数值
//    NSLog(@"现在是%ld年" , comp.year);
//    NSLog(@"现在是%ld月 " , comp.month);
//    NSLog(@"现在是%ld日" , comp.day);
//    NSLog(@"现在是%ld时" , comp.hour);
//    NSLog(@"现在是%ld分" , comp.minute);
//    NSLog(@"现在是%ld秒" , comp.second);
//    NSLog(@"现在是星期%ld" , comp.weekday);
    // 再次创建一个NSDateComponents对象
//    NSDateComponents* comp2 = [[NSDateComponents alloc]
//                               init];
    // 设置各时间字段的数值
//    comp2.year = 2013;
//    comp2.month = 4;
//    comp2.day = 5;
    comp.hour = 00;
    comp.minute = 00;
    comp.second = 00;
    // 通过NSDateComponents所包含的时间字段的数值来恢复NSDate对象
//    NSDate *date = [gregorian dateFromComponents:comp];
//    NSLog(@"获取的日期为：%@" , date);
    NSString * today = [NSString stringWithFormat:@"%ld-%ld-%ld 00:00:00",(long)comp.year,(long)comp.month,(long)comp.day];
    return today;
}
+(NSString *)todayEnd{
    // 获取代表公历的NSCalendar对象
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    // 获取各时间字段的数值
//    NSLog(@"现在是%ld年" , comp.year);
//    NSLog(@"现在是%ld月 " , comp.month);
//    NSLog(@"现在是%ld日" , comp.day);
//    NSLog(@"现在是%ld时" , comp.hour);
//    NSLog(@"现在是%ld分" , comp.minute);
//    NSLog(@"现在是%ld秒" , comp.second);
//    NSLog(@"现在是星期%ld" , comp.weekday);
    // 再次创建一个NSDateComponents对象
//    NSDateComponents* comp2 = [[NSDateComponents alloc]
//                               init];
    // 设置各时间字段的数值
    //    comp2.year = 2013;
    //    comp2.month = 4;
    //    comp2.day = 5;
    comp.hour = 00;
    comp.minute = 00;
    comp.second = 00;
    // 通过NSDateComponents所包含的时间字段的数值来恢复NSDate对象
//    NSDate *date = [gregorian dateFromComponents:comp];
//    NSLog(@"获取的日期为：%@" , date);
    NSString * today = [NSString stringWithFormat:@"%ld-%ld-%ld 23:59:59",(long)comp.year,(long)comp.month,(long)comp.day];
    return today;
}

+ (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
//    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
+ (NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}


+ (NSString *)getMonthBeginWith:(NSString *)dateStr{
    
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM"];
    NSDate *newDate=[format dateFromString:dateStr];
    double interval = 0;
    NSDate *beginDate = nil;
    NSDate *endDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar setFirstWeekday:2];//设定周一为周首日
    BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:newDate];
    //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [beginDate dateByAddingTimeInterval:interval-1];
    }else {
        return @"";
    }
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"YYYY-MM-dd 00:mm:ss"];
    NSString *beginString = [myDateFormatter stringFromDate:beginDate];
//    NSString *endString = [myDateFormatter stringFromDate:endDate];
//    NSString *s = [NSString stringWithFormat:@"%@-%@",beginString,endString];
    return beginString;
}
+ (NSString *)getMonthEndWith:(NSString *)dateStr{
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM"];
    NSDate *newDate=[format dateFromString:dateStr];
    double interval = 0;
    NSDate *beginDate = nil;
    NSDate *endDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar setFirstWeekday:2];//设定周一为周首日
    BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:newDate];
    //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [beginDate dateByAddingTimeInterval:interval-1];
    }else {
        return @"";
    }
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"YYYY-MM-dd 23:59:59"];
//    NSString *beginString = [myDateFormatter stringFromDate:beginDate];
    NSString *endString = [myDateFormatter stringFromDate:endDate];
//    NSString *s = [NSString stringWithFormat:@"%@-%@",beginString,endString];
    return endString;

}

+ (NSString *) getMonth{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    return [NSString stringWithFormat:@"%ld",(long)comp.month];
}
+ (NSString *) getLastMonth{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    return [NSString stringWithFormat:@"%ld",(long)comp.month-1];
}
//获取当前日期 年月日 YYYY-MM-DD
+ (NSString *)getDateStringYearMonthDay:(NSDate *)inputDate{
    //    NSDate *now=[NSDate date];
    NSCalendar *cal=[NSCalendar currentCalendar];
    unsigned int time=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
    NSDateComponents *t=[cal components:time fromDate:inputDate];
    int year=(int)[t year];
    int mouth=(int)[t month];
    int day=(int)[t day];
//    int hour=(int)[t hour];
//    int minute=(int)[t minute];
    //int second=[t second];
    
    //    int tomorrowDay = day+1;
    //    int afterTomorrowDay = day+2;
    //    int threeDay = day+3;
    
    
    //    NSString *strDate = [HMDataProvider getDateFromDay:day];
    
    if (mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12) {
        if (day>31) {
            
            day=day-31;
            mouth+=1;
            if (mouth>12) {
                mouth = 1;
                year+=1;
            }
        }else{
            //            NSLog(@"不做任何处理");
        }
    }else if (mouth==2){
        if(year%400==0 || (year%4==0 &&year%100!=0)) {//闰年
            if (day>29) {
                day=day-29;
                mouth+=1;
            }else{
                //            NSLog(@"不做任何处理");
            }
        }else{
            if (day>28) {
                day=day-28;
                mouth+=1;
            }else{
                //            NSLog(@"不做任何处理");
            }
            
        }
        
    }else{
        if (day>30) {
            day=day-30;
            mouth+=1;
        }else{
            //            NSLog(@"不做任何处理");
        }
    }
    //       NSString *strNew =[NSString stringWithFormat:@"预计%2d-%2d-%2d %2d:%2d前到账",year,mouth,day,hour,minute];
    NSString *strNew =[NSString stringWithFormat:@"%d-%.2d-%.2d",year,mouth,day];
    //    NSLog(@"text:%@",strNew);
    
    return strNew;
}
//日期转换星期
#pragma mark -date weekday
+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    //    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}
//获取当前日期的下一天日期
+ (NSString *)getDateFromDay:(NSDate *)inputDate {
    
    //    NSDate *now=[NSDate date];
    NSCalendar *cal=[NSCalendar currentCalendar];
    unsigned int time=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
    NSDateComponents *t=[cal components:time fromDate:inputDate];
    int year=(int)[t year];
    int mouth=(int)[t month];
    int day=(int)[t day];
//    int hour=(int)[t hour];
//    int minute=(int)[t minute];
    //int second=[t second];
    
    //    day = day-1;
    //    int tomorrowDay = day+1;
    //    int afterTomorrowDay = day+2;
    //    int threeDay = day+3;
    
    
    //    NSString *strDate = [HMDataProvider getDateFromDay:day];
    
    if (mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12) {
        if (day>31) {
            day=day-31;
            mouth+=1;
            if (mouth>12) {
                mouth = 1;
                year+=1;
            }
        }else{
            //            NSLog(@"不做任何处理");
        }
    }else if (mouth==2){
        if(year%400==0 || (year%4==0 &&year%100!=0)) {//闰年
            if (day>29) {
                day=day-29;
                mouth+=1;
            }else{
                //            NSLog(@"不做任何处理");
            }
        }else{
            if (day>28) {
                day=day-28;
                mouth+=1;
            }else{
                //            NSLog(@"不做任何处理");
            }
            
        }
        
    }else{
        if (day>30) {
            day=day-30;
            mouth+=1;
        }else{
            //            NSLog(@"不做任何处理");
        }
    }
    //       NSString *strNew =[NSString stringWithFormat:@"预计%2d-%2d-%2d %2d:%2d前到账",year,mouth,day,hour,minute];
    //    NSString *strNew =[NSString stringWithFormat:@"%2d-%.2d-%.2d",year,mouth,day];
    //    NSLog(@"text:%@",strNew);
    NSString *strDate = [NSString stringWithFormat:@"%d年%.2d月%.2d日",year,mouth,day];
    return strDate;
    
}

@end

@implementation UILabel(ZXToolObj)

+ (UILabel *)createLabelWithFrame:(CGRect)frame font:(UIFont *)font{
    UILabel *lbl = [[UILabel alloc] initWithFrame:frame];
    lbl.font = font;
    lbl.backgroundColor = [UIColor clearColor];
    
    return lbl;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame font:(UIFont *)font textColor:(UIColor *)color{
    UILabel *lbl = [self createLabelWithFrame:frame font:font];
    lbl.textColor = color;
    
    return lbl;
}

+ (UILabel *)createLabelWithAutoLayoutFont:(UIFont *)font textColor:(UIColor *)color{
    UILabel *lbl = [self createLabelWithFont:font];
    lbl.textColor = color;
    
    return lbl;
}

+ (UILabel *)createLabelWithFont:(UIFont *)font{
    UILabel *lbl = [[UILabel alloc] init];
    lbl.font = font;
    lbl.backgroundColor = [UIColor clearColor];
    
    return lbl;
}

@end


